package w83b.w83bOperacionesPaso.w83bComunicaciones;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import w83b.w83bBeans.W83bDocBean;
import es.ejie.frmk.presentacion.operacion.paso.Q70ActionPasoOP;
import es.ejie.frmk.presentacion.sesiondatos.Q70ContextoEJIE;

public class W83bGenerarRtfComunicDefAction extends Q70ActionPasoOP{


	
	protected void processOP(Q70ContextoEJIE contexto, ActionErrors errores, ActionMapping mapping,
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		
	}

	protected ActionForward processForward(Q70ContextoEJIE contexto, ActionErrors errors, ActionMapping mapping, 
			ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		HashMap generarRtf=(HashMap)contexto.get("generarRtf");
		List datos=(ArrayList)contexto.get("documento");
		//Lo Casteo a list y no a ArrayList xq es un RandomAccessSublist
		List comunicind=(List)generarRtf.get("comunicind");
		response.setContentType("application/msword");
	    response.setContentLength(Integer.parseInt((String)datos.get(datos.size()-1)));
	    datos.remove(datos.size()-1);
		
	    if ("preaviso".equals(((W83bDocBean)comunicind.get(0)).getEscomunicacion())){
	    	response.setHeader("Content-disposition","attachment;filename=ComunicacionPreavisos.rtf");
	    }else if ("comempresa".equals(((W83bDocBean)comunicind.get(0)).getEscomunicacion())){
	    	response.setHeader("Content-disposition","attachment;filename=ComunicacionEmpresas.rtf");
	    }else if ("impug".equals(((W83bDocBean)comunicind.get(0)).getEscomunicacion())){
	    	response.setHeader("Content-disposition","attachment;filename=ComunicacionImpugnaciones.rtf");
	    }else if ("laudo".equals(((W83bDocBean)comunicind.get(0)).getEscomunicacion())){
	    	response.setHeader("Content-disposition","attachment;filename=ComunicacionLaudos.rtf");
	    }else if ("requemesa".equals(((W83bDocBean)comunicind.get(0)).getEscomunicacion())){
	    	response.setHeader("Content-disposition","attachment;filename=ComunicacionRequerimiento.rtf");
	    }else if ("comunicacion".equals(((W83bDocBean)comunicind.get(0)).getEscomunicacion())){
	    	response.setHeader("Content-disposition","attachment;filename=ComunicacionIncidencias.rtf");
	    }

	    
	    for(int i=0,size=datos.size();i<size;i++){
	    	response.getOutputStream().write(datos.get(i).toString().getBytes());	    	
	    }
	    
	  
	    response.getOutputStream().flush();
	    contexto.set("generarRtf",null);
	    contexto.set("documento",null);
	    System.runFinalization();
	    System.gc();	 
	    return null;
	}
	
		
}

